public class test {
    public static int binarySearch(int[]arr,int key){
        int left=0;
        int right=arr.length-1;
        while(left<=right)
        {
            int mid=left+(right-left)/2;
            if(arr[mid]==key)
            {
                return mid;
            }
            else if(arr[mid]>key)
            {
                right=mid-1;
            }
            else
            {
                left=mid-1;
            }
        }
        return -1;
    }
    public static void main(String[] args) {
        int[]arr={1,2,3,4,5};
        int key=3;
        int ret=binarySearch(arr,key);
        if(ret==-1)
        {
            System.out.println("未找到数组中有该下标的数字");
        }
        else
        {
            System.out.println(ret);
        }
        }
}
